package com.think.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * Created by Administrator on 2018/5/21 0021.
 */
@RestController
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private RedisTemplate redisTemplate;


    @PreAuthorize("hasRole('ROOT')")
    @RequestMapping(value = "/root",method = {RequestMethod.POST,RequestMethod.GET})
    public String index(){
        if (redisTemplate.hasKey("access")) {
            logger.info("access: " + redisTemplate.opsForValue().get("access"));
        }
        return "home";
    }

    @GetMapping("/login_failure")
    public String loginFailure() {
        return "login failure";
    }

    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @RequestMapping(value = "/api/admin",method = {RequestMethod.POST,RequestMethod.GET})
    public String hello(){
        return "hello admin";
    }

    @PreAuthorize("hasAuthority('ROLE_USER')")
    @RequestMapping(value = "/api/user",method = {RequestMethod.POST,RequestMethod.GET})
    public Principal user(Principal user){
        return user;
    }

    @PreAuthorize("hasRole('ROLE_USER')")
    @RequestMapping(value = "/user/hello",method = {RequestMethod.POST,RequestMethod.GET})
    public String helloUser(){
        return "hello user";
    }
}